package com.virtualmall.utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang.StringUtils;

public class ISOUtils {
    public static byte[] hexString2Bytes(String hexstr) {
        if (hexstr.length() == 1)
            return new byte[] { (new Integer(hexstr)).byteValue() };
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;
        for (int i = b.length - 1; i >= 0; i--) {
            char c0 = hexstr.charAt(j++);
            char c1 = hexstr.charAt(j++);
            b[i] = (byte) ((parse(c0) << 4) | parse(c1));
        }
        return b;
    }

    private static int parse(char c) {
        if (c >= 'a')
            return (c - 'a' + 10) & 0x0f;
        if (c >= 'A')
            return (c - 'A' + 10) & 0x0f;
        return (c - '0') & 0x0f;
    }

    public static String bytes2HexString(byte[] b) {
        StringBuffer d = new StringBuffer(b.length * 2);
        for (int i = b.length - 1; i >= 0; i--) {
            char hi = Character.forDigit((b[i] >> 4) & 0x0F, 16);
            char lo = Character.forDigit(b[i] & 0x0F, 16);
            d.append(Character.toUpperCase(hi));
            d.append(Character.toUpperCase(lo));
        }
        return d.toString();
    }

    public static byte[] int2bytes(int n) {
        byte[] ab = new byte[4];
        ab[0] = (byte) (0xff & n);
        ab[1] = (byte) ((0xff00 & n) >> 8);
        ab[2] = (byte) ((0xff0000 & n) >> 16);
        ab[3] = (byte) ((0xff000000 & n) >> 24);
        return ab;
    }

    public static byte[] short2Bytes(short value) {

        byte[] byteArrayData = new byte[2];
        byteArrayData[0] = (byte) (value & 0xff);
        byteArrayData[1] = (byte) ((value >> 8) & 0xff);

        return byteArrayData;
    }

    public static byte[] short1Bytes(short value) {

        byte[] byteArrayData = new byte[1];
        byteArrayData[0] = (byte) (value & 0xff);

        return byteArrayData;
    }
    
    public static short bytes2Short(byte[] s) {
        short r = 0;
        for (int i = s.length - 1; i >= 0; i--) {
            r <<= 8;
            r |= (s[i] & 0x00ff);
        }
        return r;
    }

    public static int bytes2Int(byte[] bytes) {
        int addr = bytes[0] & 0xFF;
        addr |= ((bytes[1] << 8) & 0xFF00);
        addr |= ((bytes[2] << 16) & 0xFF0000);
        addr |= ((bytes[3] << 24) & 0xFF000000);
        return addr;
    }

    public static byte[] ip2bytes(String strIp) {

        String[] ipArray = strIp.split("\\.");
        byte[] ip = new byte[ipArray.length];

        for (int i = 0; i < ip.length; i++) {
            ip[i] = hexString2Bytes(Integer.toHexString(Integer
                    .parseInt(ipArray[i])))[0];
        }
        return ip;
    }
    /**
     * 返回字符串转换字节数组
     * @param str
     * @return
     */
    public static byte[] getString2Bytes(String str) {
        byte[] commentData = new byte[0];
        if (StringUtils.isNotEmpty(str)) {
            try {
                byte[] commentBytes = str.getBytes("UTF-16");
                if(commentBytes[0] == -2 && commentBytes[1] == -1) {
                    byte[] cb = new byte[commentBytes.length - 2];
                    for (int i = 0; i < cb.length; i ++) {
                        cb[i] = commentBytes[i + 2];
                    }
                    commentBytes = cb;
                }
                short commsLen = (short) (commentBytes.length / 2 + 1);
                byte[] commentL = ISOUtils.short2Bytes(commsLen);
                commentData = new byte[2 + commsLen * 2];
                commentData[0] = commentL[0];
                commentData[1] = commentL[1];
                for (int i = 0; i < commentBytes.length; i +=2) {
                    commentData[2 + i] = commentBytes[i + 1];
                    commentData[2 + i + 1] = commentBytes[i];
                }
            } catch (Exception e) {
                commentData = new byte[0];
            }
        }
        return commentData;
    }
    
    /**
     * 根据byte数组返回指定位置字符串
     * @param bArr byte数组
     * @param starLen 开始位置 ，前两位表示字符串长度
     * @return
     */
    public static String getByte2String(byte[] bArr, int starLen){
        String str = "";
        try {
            int len = ISOUtils.bytes2Short(new byte[] {bArr[starLen], bArr[starLen+1]});
            len --;
            if(len > 0){
                byte[] msgB = new byte[len*2];
                for (int i = 0; i < len*2; i +=2) {//两位表示一个汉字，高低位交换
                    msgB[i] = bArr[starLen +2 + i +1];
                    msgB[i+1] = bArr[starLen +2 + i];
                }
                str = new String(msgB, "utf-16");
            }
        } catch (UnsupportedEncodingException e) {
        }
        return str;
    }
    
    public static byte[] getString2AnsiBytes(String str){
        byte[] commentData = new byte[0];
        if (StringUtils.isNotEmpty(str)) {
            try {
                byte[] commentBytes = str.getBytes();
                short  commsLen = (short)(commentBytes.length + 1)  ;
                byte[] commentL = ISOUtils.short2Bytes(commsLen);
                commentData = new byte[2 + commsLen];
                commentData[0] = commentL[0];
                commentData[1] = commentL[1];
                for (int i = 0; i < commentBytes.length; i ++) {
                    commentData[2 + i] = commentBytes[i];   
                }
            } catch (Exception e) {
                commentData = new byte[0];
            }
        }
        return commentData;
    }
}
